/**
 * Created by fupeng on 17/3/6.
 */
// var dao = require('../dao/CommentDao');
var async = require('async');
var tool = require('../../common/utils/appUtils');
var moment = require('moment');
var request = require('request');

module.exports = {
    scan: function (params, callback) {
        var openid = params[0];
        var fromid = parseInt(params[1]);
        var tagid = 0;
        // 获取标签
        // 再验证
        async.waterfall([
                function (callback) {
                    tool.baseDao('', 'select access_token from gdas_wechat_access_token where id=1', null, function (result) {
                        callback(null, result.rows[0].access_token)
                    })
                },
                function (arg, callback) {
                    //查找标签
                    var url = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token=' + arg;

                    request({
                        url: url,
                        method: "POST",
                        json: true,
                        headers: {
                            "content-type": "application/json",
                        },
                        body: {
                            "expire_seconds": 604800,
                            "action_name": "QR_SCENE",
                            "action_info": {"scene": {"scene_id": '推广链接'+fromid}}
                        }
                    }, function (error, response, body) {
                        console.log(JSON.stringify(response));

                        if (body.hasOwnProperty('tags') && body.tags.length > 0) {
                            for (var i = 0; i < body.tags.length; i++) {
                                if (body.tags[i].name === '推广链接'+fromid) {
                                    tagid = body.tags[i].id;
                                    callback(null, arg,tagid)
                                } else {

                                }
                            }
                        }
                    });
                }, function (arg,tagid, callback) {
                    //给用户添加标签
                    var url = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=' + arg;

                    request({
                        url: url,
                        method: "POST",
                        json: true,
                        headers: {
                            "content-type": "application/json",
                        },
                        body: {
                            "openid_list": [
                                openid
                            ],
                            "tagid": tagid
                        }
                    }, function (error, response, body) {
                        console.log(JSON.stringify(response));
                        callback(null,body.errmsg)
                    });
                }
            ],
            function (err, result) {

            }
        )
    }
}